import { Component } from '@angular/core';
import { Router, Params } from '@angular/router';
import { ForgotService } from '../../service/forgot.service';

declare let $:any;
declare let cookie:any;

@Component({
    selector: 'forgot',
    templateUrl: '../../../views/forgot_password/forgot.html',
    styleUrls: ['../../../css/forgot_password/forgot.css'],
})

export class ForgotComponent {
    public mobile:any;
    public code:any;
    public password:any;
    public password_confirmation:any;
    public isPhone :any = /^1(3|4|5|7|8)[0-9]\d{8}$/;

    constructor(
        private ForgotService : ForgotService,
        private router : Router
    ){
        this.init();
    }
    private init(){
        console.log("first");
    }

    // 发送验证码
    public sMsg(){
        console.log(this.mobile);
        if (this.mobile == "" || this.mobile == null){
            alert("手机号不能为空")
        }else if(this.isPhone.test(this.mobile)){
            console.log(this.mobile);
            this.getMsg();
        }else alert("手机号格式不对");

    }
    private  getMsg(){
        let self : any = this;
        let data : any = {
            mobile: self.mobile
        };
        self.ForgotService.sendModifyMsg({
            data: data,
            success: function (result : any) {
                if(result.error === 0){
                    console.log(result.message);
                }else {
                    console.log(result.msg);
                }
            }
        });
    }

    // 修改密码
    public submit(){
        console.log(this.mobile);
        if (this.mobile == "" || this.mobile == null){
            alert("手机号不能为空")
        }else if(this.isPhone.test(this.mobile)){
            console.log(this.mobile);
            this.modifyPassW();
        }else alert("手机号格式不对");
    }

    private  modifyPassW(){
        let self : any = this;
        let data : any = {
            code: self.code,
            password: self.password,
            password_confirmation: self.password_confirmation,
            mobile: self.mobile
        };
        self.ForgotService.newPassword({
            data: data,
            success: function (result : any) {
                if(result.error === 0){
                    alert("成功");
                    self.router.navigate(['/login']);
                }else {
                    alert(result.msg);
                }
            }
        });
    }
    public psd_status(index:any){
        let passwordeye :any;
        let showPwd :any;
        if(index==1)
        {
            showPwd = $("#inputPassword3");
            passwordeye = $('.psd_status1');
        }
        if(index==2)
        {
            showPwd = $("#inputPassword2");
            passwordeye = $('.psd_status2');
        }
        if(passwordeye.hasClass('invisible')){
            passwordeye.removeClass('invisible').addClass('visible');//密码可见
            showPwd.prop('type','text');
        }else{
            passwordeye.removeClass('visible').addClass('invisible');//密码不可见
            showPwd.prop('type','password');
        };
    }

}

